<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>YP.ShO购</title>
<link rel="stylesheet" href="css/bootstrap.css"></link>
<link rel="stylesheet" href="css/style.css"></link>
<link rel="stylesheet" href="css/myButton.css"></link>
<link rel="stylesheet" href="css/sellerStyle.css"></link>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.easydropdown.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript">
function showEdit() {
	var input = document.getElementById("goodsInfor").getElementsByTagName("input");
	for (var i = 0; i < input.length; i++) {
		if (input[i].type == "text") {
			input[i].readOnly = false;
		}
	}
	var select = document.getElementsByTagName("select");
	for (var i = 0; i < select.length; i++) {
		select[i].disabled = false;
	}
	$("#goodsRemark")[0].readOnly = false;
	document.getElementById('goodsInforSubmit').style = "visibility: visible";
	document.getElementById('file').style = "visibility: visible";
}

function goodsInfoCheck() {
	var goodsName = $("#goodsName").val();
	var goodsPrice = $("#goodsPrice").val();
	var goodsRemark = $("#goodsRemark").val();
	var goodsBrand = $("#goodsBrand").val();
	var goodsType = $("#goodsType").val();
	var goodsIsDelete = $("#goodsIsDelete").val();
	if(goodsName.length<1||goodsName.length>100) {
		alert("请输入不超过100位的商品名！");
		$("#goodsName").focus();
		$("#goodsName").val("${goods.goodsname}");
		return false;
	}
	if(goodsPrice.length<1||goodsPrice.length>8||isNaN(goodsPrice)) {
		alert("请输入不超过8位的数字售价！");
		$("#goodsPrice").focus();
		$("#goodsPrice").val("${goods.price}");
		return false;
	}
	if(goodsRemark.length<1) {
		alert("商品备注不能为空！");
		$("#goodsRemark").focus();
		$("#goodsRemark").val("${goods.remark}");
		return false;
	}
	if(goodsRemark.length>4096) {
		alert("商品备注不能超过4096个字节");
		$("#goodsRemark").focus();
		$("#goodsRemark").val("${goods.remark}");
		return false;
	}
	if(goodsBrand=="") {
		alert("请选择商品品牌！");
		$("#goodsBrand").focus();
		$("#goodsBrand").val("${goods.brand.id}");
		return false;
	}
	if(goodsType=="") {
		alert("请选择硬盘类型！");
		$("#goodsType").focus();
		$("#goodsType").val("${goods.type}");
		return false;
	}
	if(goodsIsDelete=="") {
		alert("请选择是否上架！");
		$("#goodsIsDelete").focus();
		$("#goodsIsDelete").val("${goods.isDelete}");
		return false;
	}
	if (confirm('您是否要修改商品信息？')) {
		$("#goodsInfor").submit();
	} else {
		return false;
	}
}

function myGoodsName() {
	if($("#goodsName").val()=="") {
		$("#goodsName").val("【"+$("#goodsUser").text()+"】"
				+$("#goodsBrand").find("option:selected").text()
				+" "+$("#goodsCapacity").val()
				+" "+$("#goodsType").find("option:selected").text());
	}
}

function myGoodsRemark() {
	if($("#goodsRemark").val()=="") {
		$("#goodsRemark").val($("#goodsBrand").find("option:selected").text()
				+$("#goodsType").find("option:selected").text()+"，"
				+$("#goodsCache").val()+"缓存，"+$("#goodsPCType").val()
				+"，"+$("#goodsUser").text()+"出品！");
	}
}

function fileSubmit() {
	var formData = new FormData($("#file")[0]);
	$.ajax({
		url : "fileUpload.action",
		type : "post",
		data : formData,
		contentType: false,
		processData: false,  
	    dataType: "json", 
		success : function(data) {
			var datas = JSON.parse(data);
			var message = datas.message;
			var flag = datas.flag;
			var imgPath = datas.imgPath;
			alert(message);
			if(flag == "success"){
				$("#goodsImg").attr("src",imgPath);
				$("#imgPath").val(imgPath);
			}
		},
		error : function() {
			alert("与服务器同步失败");
		}
	}); 
}

$(document).ready(function() {
	$("#goodsBrand").val(${goods.brand.id });
	$("#goodsIsDelete").val(${goods.isDelete });
	$("#goodsType").val(${goods.type });
	$("#goodsPrice").keyup(function(){    
        $(this).val($(this).val().replace(/[^0-9.]/g,''));    
    }).change(function(){    
        $(this).val($(this).val().replace(/[^0-9.]/g,''));    
    }).bind("paste",function(){  //CTR+V事件处理    
        $(this).val($(this).val().replace(/[^0-9.]/g,''));     
    }).css("ime-mode", "disabled"); //CSS设置输入法不可用    
})
</script>
<style type="text/css">
.form p {
	margin-bottom: 15px;
}

.form label {
	width: 8em;
}
</style>

</head>

<body>
	<%@include file="../common/head.jsp"%>
	<div class="menu"></div>
	<div class="men">
		<div class="container">
			<div class="col-md-3 sidebar">
				<div class="block block-layered-nav">
					<div class="block-title">
						<strong><span>我的YPShO购</span></strong>
					</div>
					<div class="block-content">
						<dl id="narrow-by-list">
							<dt class="last odd">用户中心</dt>
							<dd class="last odd">
								<ol>
									<li><a href="userInforView">个人信息管理</a></li>
								</ol>
							</dd>
							<dt class="last odd">我是买家</dt>
							<dd class="last odd">
								<ol>
									<li><a href="userOrders">我的订单管理</a></li>
									<li><a href="userCart">我的购物车</a></li>
								</ol>
							</dd>
							<dt class="last odd">我是卖家</dt>
							<dd class="last odd">
								<ol>
									<li><a href="sellerOrders">我的订单管理</a></li>
									<li><a href="sellerGoods" style="color:#e05b5b">我的商品管理</a></li>
								</ol>
							</dd>
						</dl>
					</div>
				</div>
			</div>
			<div class="col-md-9">
				<div class="mens-toolbar">
					<div class="sort">
						<div class="sort-by">
							<label>商品管理</label>
						</div>
					</div>
					<div class="sort" style="float:right">
						<div class="sort-by" style="float:right">
							<input type="button" class="button green medium" style="margin-top: 0px" onclick="showEdit()" value="编辑">
						</div>
					</div>
				</div>
				<div class="form">
				<form action="editGoods.action" id="goodsInfor">
					<p>
						<label>商品图片：</label>
						<s:if test="goods.img==null||goods.img==''">
						<span><img alt="请上传商品图片" src="uploadImg/goodsImg/default.jpg" width="150px" id="goodsImg"></span>
						</s:if><s:else>
						<span><img alt="请上传商品图片" src="<s:property value="goods.img"/>" width="150px" id="goodsImg"></span></s:else>
						<input type="hidden" name="goods.img" id="imgPath">
						<span class="redstar">*</span>
					</p>
					<p>
						<label>价格：</label>
						<span><input type="text" name="goods.price" value="<s:property value="goods.price"/>" id="goodsPrice" readonly="readonly"/>
						<span class="redstar">*</span></span>
					</p>
					<s:if test="goods.id==null">
					<p>
						<label>硬盘大小：</label> 
						<span> 
						<select id="goodsCapacity" disabled="disabled">
							<option value="" selected="selected">---请选择硬盘大小---</option>
							<option value="32GB">32GB</option>
							<option value="64GB">64GB</option>
							<option value="128GB">128GB</option>
							<option value="320GB">320GB</option>
							<option value="500GB">500GB</option>
							<option value="1TB">1TB</option>
						</select>
						<span class="redstar">*</span> 
						</span>
					</p>
					<p>
						<label>硬盘缓存大小：</label> 
						<span> 
						<select id="goodsCache" disabled="disabled">
							<option value="" selected="selected">---请选择硬盘缓存大小---</option>
							<option value="8MB">8MB</option>
							<option value="16MB">16MB</option>
							<option value="32MB">32MB</option>
							<option value="64MB">64MB</option>
						</select>
						<span class="redstar">*</span> 
						</span>
					</p>
					<p>
						<label>适用类型：</label> 
						<span> 
						<select id="goodsPCType" disabled="disabled">
							<option value="" selected="selected">---请选择适用于电脑类型---</option>
							<option value="笔记本专用">仅适用于笔记本</option>
							<option value="台式机专用">仅适用于台式电脑</option>
						</select>
						<span class="redstar">*</span> 
						</span>
					</p>
					</s:if>
					<p>
						<label>品牌：</label> 
						<span> 
						<select id="goodsBrand" name="goods.brand.id" disabled="disabled">
							<option value="" selected="selected">---请选择品牌---</option>
							<s:iterator value="Blist" id="brand">
								<option value="<s:property value="#brand.id"/>"><s:property value="#brand.brandname"/></option>
							</s:iterator>
						</select>
						<span class="redstar">*</span> 
						</span>
					</p>
					<p>
						<label>硬盘类型：</label> 
						<span> 
						<select id="goodsType" name="goods.type" disabled="disabled">
							<option value="" selected="selected">---请选择硬盘类型---</option>
							<s:iterator value="typeList" id="type">
								<option value="<s:property value="#type.value2"/>"><s:property value="#type.value1"/></option>
							</s:iterator>
						</select>
						<span class="redstar">*</span> 
						</span>
					</p>
					<p>
						<label>商品名称：</label>
						<span><input type="text" name="goods.goodsname" value="<s:property value="goods.goodsname"/>" id="goodsName" readonly="readonly" onclick="myGoodsName()" style="width: 450px"/></span>
						<span class="redstar">*</span>
					</p>
					<p>
						<label>简介：</label>
						<span onclick="myGoodsRemark()">
						<textarea style="height: 120px;width: 550px;vertical-align: top;" name="goods.remark" id="goodsRemark"  readonly="readonly"><s:property value="goods.remark"/></textarea>
						</span>
						<span class="redstar">*</span>
					</p>
					<p>
						<label>是否上架：</label>
							<span>
							<select id="goodsIsDelete" name="goods.isDelete" disabled="disabled">
								<option value="" selected="selected">---请选择---</option>
								<option value="0">是</option>
								<option value="1">否</option>
							</select>
							</span>
							<span class="redstar">*</span>
					</p>
					<p>
						<label>卖家：</label>
						<span>
							<%-- <input type="hidden" name="goods.user" value="${session.loginUser.id }" id="goodsUser"/> --%>
							<span id="goodsUser">${session.loginUser.name }</span>
							<span class="redstar">*</span>
						</span>
					</p>
				</form>
				<form action="fileUpload.action" enctype="multipart/form-data" method="post" id="file" style="visibility: hidden;">
					<p>
						<label>上传商品图片：</label>
				        <input type="file" id="file" name="image" style="display:inline;"/>
				        <input type="hidden" name="imgFrom" value="goodsEdit">
				        <input type="button" value="上传" class="button blue medium" style="display:inline;" onclick="fileSubmit()">
			        </p>
				</form>
					<input id="goodsInforSubmit" type="button" value="保存商品信息" onclick="goodsInfoCheck()" style="visibility: hidden;" class="button green">
				</div>
			</div>
		</div>
	</div>

	<%@include file="../common/footer.jsp"%>

	</body>
</html>
